// 切换登录弹窗显示/隐藏
function toggleLogin() {
    const modal = document.getElementById('loginModal');
    modal.classList.toggle('active');
}

// 处理登录表单提交
function handleLogin(event) {
    event.preventDefault();
    
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    const remember = document.getElementById('remember').checked;
    
    // 这里添加登录逻辑
    console.log('登录信息：', { username, password, remember });
    
    // 模拟登录请求
    setTimeout(() => {
        // 登录成功后的处理
        alert('登录成功！');
        toggleLogin();
    }, 1000);
}

// 点击弹窗外部关闭弹窗
document.getElementById('loginModal').addEventListener('click', (e) => {
    if (e.target.id === 'loginModal') {
        toggleLogin();
    }
});

// 为导航栏的登录按钮添加点击事件
document.querySelector('.login-btn').addEventListener('click', toggleLogin);

// 切换注册弹窗显示/隐藏
function toggleRegister() {
    const modal = document.getElementById('registerModal');
    modal.classList.toggle('active');
}

// 从登录切换到注册
function switchToRegister(event) {
    event.preventDefault();
    toggleLogin();
    setTimeout(() => {
        toggleRegister();
    }, 300);
}

// 从注册切换到登录
function switchToLogin(event) {
    event.preventDefault();
    toggleRegister();
    setTimeout(() => {
        toggleLogin();
    }, 300);
}

处理注册表单提交
function handleRegister(event) {
    event.preventDefault();

    const username = document.getElementById('reg-username').value;
    const email = document.getElementById('reg-email').value;
    const phone = document.getElementById('reg-phone').value;
    const phoneCode = document.getElementById('phone-code').value;
    const captcha = document.getElementById('captcha').value;
    const password = document.getElementById('reg-password').value;
    const confirmPassword = document.getElementById('reg-confirm-password').value;
    const agreeTerms = document.getElementById('agree-terms').checked;

    // 表单验证
    if (!/^1[3-9]\d{9}$/.test(phone)) {
        alert('请输入正确的手机号码');
        return;
    }

    if (phoneCode.length !== 6) {
        alert('请输入6位短信验证码');
        return;
    }

    if (captcha.length !== 4) {
        alert('请输入4位图形验证码');
        return;
    }

    if (password !== confirmPassword) {
        alert('两次输入的密码不一致！');
        return;
    }

    if (!agreeTerms) {
        alert('请同意服务条款和隐私政策！');
        return;
    }

    // 验证密码强度
    if (!isPasswordStrong(password)) {
        alert('密码强度不够！请包含大小写字母、数字和特殊字符，长度至少8位。');
        return;
    }

    // 这里添加注册逻辑
    console.log('注册信息：', {
        username,
        email,
        phone,
        phoneCode,
        captcha,
        password,
        agreeTerms
    });

    // 模拟注册请求
    setTimeout(() => {
        alert('注册成功！');
        toggleRegister();
        setTimeout(() => {
            toggleLogin();
        }, 500);
    }, 1000);
}

密码强度检查
function isPasswordStrong(password) {
    const minLength = 8;
    const hasUpperCase = /[A-Z]/.test(password);
    const hasLowerCase = /[a-z]/.test(password);
    const hasNumbers = /\d/.test(password);
    const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password);
    
    return (
        password.length >= minLength &&
        hasUpperCase &&
        hasLowerCase &&
        hasNumbers &&
        hasSpecialChar
    );
}

// 更新密码强度指示器
function updatePasswordStrength(password) {
    const strengthMeter = document.querySelector('.strength-meter');
    const hasLength = password.length >= 8;
    const hasUpperCase = /[A-Z]/.test(password);
    const hasLowerCase = /[a-z]/.test(password);
    const hasNumbers = /\d/.test(password);
    const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password);
    
    const strength = [hasLength, hasUpperCase, hasLowerCase, hasNumbers, hasSpecialChar]
        .filter(Boolean).length;
    
    strengthMeter.className = 'strength-meter';
    if (strength < 3) {
        strengthMeter.classList.add('strength-weak');
    } else if (strength < 5) {
        strengthMeter.classList.add('strength-medium');
    } else {
        strengthMeter.classList.add('strength-strong');
    }
}

// 发送验证码功能
function sendVerificationCode() {
    const phoneInput = document.getElementById('reg-phone');
    const sendButton = document.querySelector('.send-code-btn');
    const phone = phoneInput.value;
    
    // 验证手机号格式
    if (!/^1[3-9]\d{9}$/.test(phone)) {
        alert('请输入正确的手机号码');
        return;
    }
    
    // 禁用按钮并开始倒计时
    let countdown = 60;
    sendButton.disabled = true;
    sendButton.classList.add('counting');
    
    const timer = setInterval(() => {
        sendButton.textContent = `${countdown}秒后重试`;
        countdown--;
        
        if (countdown < 0) {
            clearInterval(timer);
            sendButton.disabled = false;
            sendButton.classList.remove('counting');
            sendButton.textContent = '发送验证码';
        }
    }, 1000);
    
    // 这里添加发送验证码的API调用
    console.log('发送验证码到：', phone);
    // 示例：发送请求到后端
    // fetch('/api/send-code', {
    //     method: 'POST',
    //     headers: { 'Content-Type': 'application/json' },
    //     body: JSON.stringify({ phone })
    // });
}

// 刷新图形验证码
function refreshCaptcha() {
    const captchaImg = document.getElementById('captchaImage');
    // 添加时间戳参数避免缓存
    captchaImg.src = 'captcha.php?' + new Date().getTime();
}

// 添加事件监听器
document.addEventListener('DOMContentLoaded', () => {
    // 为注册链接添加点击事件
    document.querySelector('.register-link a').addEventListener('click', switchToRegister);
    
    // 监听密码输入以更新强度指示器
    const passwordInput = document.getElementById('reg-password');
    if (passwordInput) {
        passwordInput.addEventListener('input', (e) => {
            updatePasswordStrength(e.target.value);
        });
    }
    
    // 点击弹窗外部关闭弹窗
    document.getElementById('registerModal').addEventListener('click', (e) => {
        if (e.target.id === 'registerModal') {
            toggleRegister();
        }
    });
    
    // 为图形验证码添加点击刷新功能
    document.getElementById('captchaImage').addEventListener('click', refreshCaptcha);
    
    // 手机号输入验证
    document.getElementById('reg-phone').addEventListener('input', (e) => {
        e.target.value = e.target.value.replace(/\D/g, '').slice(0, 11);
    });
    
    // 短信验证码输入验证
    document.getElementById('phone-code').addEventListener('input', (e) => {
        e.target.value = e.target.value.replace(/\D/g, '').slice(0, 6);
    });
}); 